package com.platform.util.wechat;

import com.jfinal.kit.Prop;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.platform.util.CommonUtil.getProp;

public class CheckUtil {

    static Prop prop = getProp();

    public static boolean checkSignature(String signature, String timestamp, String nonce) {
        String[] str = new String[]{prop.get("token"), timestamp, nonce};
        //排序
        Arrays.sort(str);
        //拼接字符串
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < str.length; i++) {
            buffer.append(str[i]);
        }
        //进行sha1加密
        String temp = SHA1.encode(buffer.toString());
        //与微信提供的signature进行匹对
        return signature.equals(temp);
    }

    public static boolean checkSuffixFormat(String url) {
        String reg = ".+(.JPEG|.jpeg|.JPG|.jpg|.png|.PNG|.GIF|.gif|.BMP|.bmp)$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            return true;
        }
        return false;
    }


}
